﻿using Dapper;
using log4net;
using StoreProject.DBUtil;
using StoreProject.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoreProject.DAL
{
    public class ChangefulDAL
    {
        ILog _log = LogManager.GetLogger("ChangefulDAL");
        public ChangefulDAL()
        {

        }
        public IDbConnection Conn
        {
            get
            {
                return MySqlHelper.Conn;
            }
        }
        public int Update(ChangefulEntity model)
        {
            try
            {
                using (this.Conn)
                {
                    int update_Result = this.Conn.Update(model);
                    return update_Result;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Update", ex);
                return 0;
            }
        }

        public ChangefulEntity Get(string id)
        {
            ChangefulEntity model = null;
            try
            {
                using (this.Conn)
                {
                    model = this.Conn.Get<ChangefulEntity>(id);
                }
            }
            catch (Exception ex)
            {
                _log.Error("Get", ex);
            }
            return model;
        }

        public bool IsExists(string id)
        {
            bool isExist = false;
            using (this.Conn)
            {
                ChangefulEntity model = Get(id);
                isExist = (model == null) ? false : true;
            }
            return isExist;
        }

        public int Delete(string id)
        {
            using (Conn)
            {
                ChangefulEntity model = new ChangefulEntity();
                model.Id = id;
                return Conn.Delete(model);
            }
        }

        public int Delete(ChangefulEntity model)
        {
            using (Conn)
            {
                return Conn.Delete(model);
            }
        }

        public IEnumerable<ChangefulEntity> GetList()
        {
            using (Conn)
            {
                return Conn.GetList<ChangefulEntity>();
            }
        }

        public IEnumerable<ChangefulEntity> GetListEx()
        {
            using (Conn)
            {
                string sql = "select * from Changeful";
                return Conn.Query<ChangefulEntity>(sql);
            }
        }
    }
}
